<template>
  <HModal @mousedown.native.stop :width="800" :height="800" @ok="handleOK"
          @cancel="close" :visible="visible" title="清空字段">
    <div class="clear-field-selector">
      <div class="clear-field-selector-left">
        <a-input-search v-model="searchText" placeholder="请输入关键字" style="margin-bottom: 5px" />
        <select multiple="true" size="10" v-model="leftSelected">
          <option @dblclick="() => addToRightItem(item)"
            v-for="item in searchedFields" :value="item.fieldId">
            {{ item.title }}
          </option>
        </select>
      </div>
      <div class="clear-field-selector-center">
        <div class="clear-field-selector-center-button" @click="addToRight"
             style="margin-bottom: 60px;"><span class="iconfont">&#xeb92;</span></div>
        <div class="clear-field-selector-center-button" @click="removeValues"><span class="iconfont">&#xeb8f;</span></div>
      </div>
      <div class="clear-field-selector-right">
        <select multiple="true" size="10" v-model="rightSelected">
          <option @dblclick="() => removeRightItem(item)"
            v-for="item in values" :value="item">
            {{ getFieldLabel(item) }}
          </option>
        </select>
      </div>
    </div>
  </HModal>
</template>
<script>
import HModal from '@comp/hhd/HModal/HModal.vue'
import { createFormDefinition, getFormDefinitionVariables } from '@comp/hhd/design/FormDesigner/types'
import { createProps } from '@/utils'

export default {
  name: 'ClearFieldInputModal',
  components: {
    HModal
  },
  props: {
    formDefinition: createProps(Object, createFormDefinition)
  },
  data() {
    return {
      searchText: undefined,
      leftSelected: [],
      rightSelected: [],
      visible: false,
      values: []
    }
  },
  methods: {
    removeRightItem(item) {
      this.values = this.values.filter(fieldId => fieldId !== item)
    },
    addToRightItem(item) {
      this.values.push(item.fieldId)
      this.clearLeftSelected()
    },
    clearLeftSelected() {
      this.leftSelected = []
    },
    removeValues() {
      this.values = this.values.filter(fieldId => !this.rightSelected.includes(fieldId))
      this.rightSelected = []
    },
    addToRight() {
      let list = this.leftSelected || []
      this.values.push(...JSON.parse(JSON.stringify(list)))
      this.clearLeftSelected()
    },
    setData(ids) {
      this.values = ids && ids.split(',') || []
      this.open()
    },
    getFieldLabel(item) {
      return (this.dataSource.find(f => f.fieldId === item) || {}).title
    },
    open() {
      this.visible = true
    },
    handleOK() {
      this.$emit('ok', this.values)
      this.close()
    },
    close() {
      this.visible = false
    }
  },
  computed: {
    searchedFields() {
      if (!this.formDefinition) return []
      let res = this.dataSource.filter(variable => !this.values.includes(variable.fieldId))
      if (this.searchText) res = res.filter(variable => variable.title.includes(this.searchText))
      return res
    },
    dataSource() {
      return getFormDefinitionVariables(this.formDefinition)
    }
  }
}
</script>
<style>
.clear-field-selector {
  display: flex;
  height: 100%;
}

.clear-field-selector-center {
  width: 50px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  user-select: none;
}

.clear-field-selector-right select,
.clear-field-selector-left select {
  flex: 1;
  outline: none;
  border: 1px solid #d5d5d5;
  overflow: auto;
  user-select: none;
}

.clear-field-selector-right,
.clear-field-selector-left {
  display: flex;
  flex-direction: column;
}

.clear-field-selector-left,
.clear-field-selector-right {
  flex: 0.5;
}

.clear-field-selector-center-button .iconfont {
  font-size: 35px;
}
.clear-field-selector-center-button {
  width: 25px;
  height: 25px;
  color: #6aa1d8 !important;
  display: flex;
  align-items: center;
  border-radius: 27px;
  justify-content: center;
  margin-bottom: 8px;
  cursor: pointer;
}

</style>